import pika

# RabbitMQ 连接参数
RABBITMQ_CONFIG = {
    "host": "10.0.106.15",
    "port": 5672,
    "username": "root",
    "password": "avs123456.",
}

# 构建连接参数对象
credentials = pika.PlainCredentials(
    username=RABBITMQ_CONFIG["username"], password=RABBITMQ_CONFIG["password"]
)
connection_params = pika.ConnectionParameters(
    host=RABBITMQ_CONFIG["host"], port=RABBITMQ_CONFIG["port"], credentials=credentials
)

# 建立连接
connection = pika.BlockingConnection(connection_params)
channel = connection.channel()

# 声明一个队列（发布前需要确保队列存在）
queue_name = "test_queue"
channel.queue_declare(queue=queue_name, durable=True)

# 发布消息
message = "Hello from pika publisher!"
channel.basic_publish(
    exchange="",  # 默认交换机
    routing_key=queue_name,  # 目标队列
    body=message.encode("utf-8"),
    properties=pika.BasicProperties(delivery_mode=2),  # 消息持久化
)

print(f" [x] Sent message: {message}")

# 关闭连接
connection.close()
